package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {

        //因为页面传过来的数据是不完整的，所以需要我们重新处理完整，再插入
        //还有创建时间，状态统一设置为预约中即可

        //创建时间
        appointment.setCreateTime(new Date());

        //设置状态 预约中
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);

        //设置为未删除的状态
        appointment.setRemoveStatus(Appointment.remove_on);

        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {

        //修改操作，需要判断当前状态是否为预约状态，否则抛出异常
        Appointment oldAppointment = appointmentMapper.selectByPrimaryKey(appointment.getId());

        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())) {
            //不是在预约状态下，抛出异常
            throw new BusinessException("只有在预约状态下，才能修改信息");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId,Appointment.remove_off);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrival(Long id) {

        //用户点到店操作，需要判断当前用户的状态为预约状态才可以操作
        //修改操作，需要判断当前状态是否为预约状态，否则抛出异常
        Appointment oldAppointment = appointmentMapper.selectByPrimaryKey(id);

        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())) {
            //不是在预约状态下，抛出异常
            throw new BusinessException("只有在预约状态下，用户才能到店");
        }

        //设置到店时间
        appointmentMapper.updateArrival(id,Appointment.STATUS_ARRIVAL,new Date());
    }

    @Override
    public void cancel(Long id) {

        //只有在预约状态下，才能取消到店操作
        Appointment oldAppointment = appointmentMapper.selectByPrimaryKey(id);

        if (!Appointment.STATUS_APPOINTMENT.equals(oldAppointment.getStatus())) {
            //不是在预约状态下，抛出异常
            throw new BusinessException("只有在预约状态下，才能取消到店操作");
        }

        appointmentMapper.updateCancel(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void changeStatus(Long id, Integer statusSettle) {

        appointmentMapper.changeStatus(id,statusSettle);
    }

    //超时取消，       秒 分 时日月年（年的可以省略）
    //每天23点执行一次：0 0 23 * * ?
    @Scheduled(cron = "0 0 23 L * ?")
    public void time(){

        List<Appointment> appointments = appointmentMapper.selectByAppointment();
        //预约用户不为空的时候，执行以下操作
        if (appointments != null){
            for (Appointment appointment : appointments) {

                //预约时间减去当前时间，如果小于0，修改状态为超时取消
                long l = appointment.getAppointmentTime().getTime() - new Date().getTime();

                if (l <0 ){
                    //去修改状态
                    appointmentMapper.changeStatus(appointment.getId(),Appointment.STATUS_OVERTIME);
                }
            }
        }
    }
}
